package com.newcapec.servlet;

import com.newcapec.bean.UserEntity;
import com.newcapec.dao.SysUserDao;
import com.newcapec.dao.impl.SysUserDaoImpl;
import com.newcapec.service.SysUserService;
import com.newcapec.service.impl.SysUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Description:注册功能的servlet类
 * @Author:yangshupeng
 * @Date: 2022/3/15 21:00
 * @Version:1.0
 */
@WebServlet("/reg")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通知浏览器响应的内容类型
        /*req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");*/
        //获取注册表单提交的数据
        String userName = req.getParameter("userName");
        String userAccount = req.getParameter("userAccount");
        String password = req.getParameter("password");

        UserEntity userEntity = new UserEntity(userName,userAccount,password);

        SysUserService sysUserService = new SysUserServiceImpl();
        if (sysUserService.addUser(userEntity)){
            //注册成功重定向到登录界面
            resp.sendRedirect("login.jsp?type=4");
        }
    }
}
